package com.sun.module_mine.ui

import androidx.lifecycle.viewModelScope
import com.sun.module_base.app.BaseViewModel
import com.sun.module_mine.ui.timer.TimerData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.util.Calendar
import javax.inject.Inject

/**
 *@author:sunc
 *@time :2025/8/14 16:14
 *@desc: 描述
 */
@HiltViewModel
class MineViewModel @Inject constructor() : BaseViewModel() {
    private val todayMax = Calendar.getInstance().apply {
        set(Calendar.HOUR_OF_DAY, 23)
        set(Calendar.MINUTE, 59)
        set(Calendar.SECOND, 59)
    }.timeInMillis

    private val _timeState =
        MutableStateFlow(TimerData(todayMax.minus(Calendar.getInstance().timeInMillis)))
//        MutableStateFlow(TimerData(Calendar.getInstance().timeInMillis))

    val timeState = _timeState.asStateFlow()

    init {
        initTimer()
    }

    private fun initTimer() {
        viewModelScope.launch {
            while (true) {
                val next = timeState.value.millis.minus(1000)
//                val next = timeState.value.millis.plus(1000)
                _timeState.emit(timeState.value.copy(millis = next))
                delay(1000L)
            }
        }
    }

}